<div ng-controller="yuangongC">
    <div class="bg-light lter b-b wrapper-md">
        <h1 class="m-n font-thin h3">员工管理</h1>
    </div>
    <div class="wrapper-md" style="text-align: center;">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-12" style="float: right">
                    <button class="btn btn-sm btn-default" ng-click="add()">
                        <i class="fa fa-plus text"></i>
                        <span class="text">添加</span>
                    </button>
                </div>
                <div class="col-lg-6 col-md-6 col-sm-6 col-xs-12 input-group">
                    <span class="input-group-addon">按名称查找</span>
                    <input type="text" class="form-control" id='findname' placeholder="按名称查找" ng-model="findname">
                    <span class="input-group-addon">按状态查找</span>
                    <select class="form-control" id="IsLeave" ng-model="IsLeave" style="min-width: 100px">
                        <option value="false" ng-selected="1">在职</option>
                        <option value="true">离职</option>
                    </select>
                    <span class="input-group-btn">
                        <button class="btn btn-default" type="button" ng-click="find()">查找</button>
                    </span>
                </div>
            </div>
            <div class="table-responsive">
                <table id="example1" class="table table-striped b-t b-light table-bordered">
                    <thead>
                        <tr>
                            <th style="text-align: center;">用户名</th>
                            <th style="text-align: center;">登录名</th>
                            <th style="text-align: center;">部门</th>
                            <th style="text-align: center;">电话</th>
                            <th style="text-align: center;">创建时间</th>
                            <th style="text-align: center;">状态</th>
                            <th style="text-align: center;">操作</th>

                        </tr>
                    </thead>
                    <tbody>
                        <tr ng-repeat="item in users">
                            <td style="width:15%">{{item.Name}}</td>
                            <td style="width:15%">{{item.LoginName}}</td>
                            <td style="width:10%">{{item.Department.Name}}</td>
                            <td style="width:10%">{{item.Phone}}</td>
                            <td style="width:20%">{{item.CreateTime}}</td>
                            <td style="width:10%" ng-if="!item.IsLeave">在职</td>
                            <td style="width:10%" ng-if="item.IsLeave">离职</td>
                            <td style="width:20%">
                                <button class="btn btn-sm btn-default" ng-click="edituserz($index)">
                                    <i class="fa fa-edit text"></i>
                                    <span class="text">编辑</span>
                                </button>
                                <button class="btn btn-sm btn-default" ng-click="gomanage($index)">
                                    <i class="fa fa-cogs text"></i>
                                    <span class="text">管理</span>
                                </button>
                            </td>

                        </tr>
                    </tbody>
                </table>
                <div class="panel-heading" my-pagination get-page-data="getData" mypagedata="pageData"></div>
            </div>
        </div>
    </div>
</div>
<script>
    angular.module('app')
        //员工管理控制器
    .controller("yuangongC", function ($scope, $state, $http, toSave) {
        $scope.pageData = {};
        $scope.pageData.Page = 0;
        $scope.findname = "";
        $scope.IsLeave = "false";
        $scope.pageData.Rows = $scope.defPageRows;
        $scope.getData = function (p, r) {
            if (p) {
                p = p < 1 ? 1 : p;
            } else p = 1;
            if (r) {
            } else r = $scope.defPageRows;
            toSave.save_pages_lis(p, r);//保存当前页码和条数
            $http({
                url: "../api/Employee/List?name=" + $scope.findname + "&isLeave=" + $scope.IsLeave + "&page=" + p + "&rows=" + r,
                method: "GET"
            })
                    .then(function (res) {
                        $scope.pageData = res.data;
                        $scope.users = res.data.Data;
                        console.log(toSave.get_pages(), toSave.get_lis());
                    })
                    .then(function (error) {
                        if (error) {
                            console.log("error");
                        }
                    });
        };
        $scope.getData(toSave.get_pages(), $scope.pageData.Rows);

        // 查找用户方法
        $scope.find = function () {
            $scope.getData(1, $scope.pageData.Rows);
            toSave.save_inputs();//点击查询先保存input里面的值
          
        };

        //添加用户方法
        $scope.add = function () {
            $state.go('app.Employee_Add');
            toSave.save_pages_lis(toSave.get_pages(), toSave.get_lis());
            console.log(toSave.get_pages(), toSave.get_lis())
        };

        //编辑用户方法
        $scope.edituserz = function (index) {
            $state.go('app.Employee_Edit', {
                userdata: JSON.stringify($scope.users[index].Id)
            });

            toSave.save_pages_lis(toSave.get_pages(), toSave.get_lis());
            console.log(toSave.get_pages(), toSave.get_lis());
        };

        //管理用户的方法
        $scope.gomanage = function (index) {
            $state.go('app.Admin_SetRoles', {
                userdata: JSON.stringify($scope.users[index].Id)
            });
            toSave.save_pages_lis(toSave.get_pages(), toSave.get_lis());
            console.log(toSave.get_pages(), toSave.get_lis());
        };
        console.log(toSave.get_pages());
        if (toSave.get_inputs('findname')) {
            $scope.findname = toSave.get_inputs('findname');//把保存该id名的值取出来赋值给input框
            console.log(toSave.get_pages());
            $scope.getData(toSave.get_pages(), toSave.get_lis());

        } else {
            $scope.getData(toSave.get_pages(), toSave.get_lis());
            console.log(toSave.get_pages());
        }
    })
</script>
<script src="js/services/toSave.js"></script>